<?php
declare (strict_types=1);

namespace app\user\logic\user\admin;

use app\common\exception\LogicException;
use app\user\event\subscribes\user\Balance as BalanceSubscribe;
use app\user\model\user\admin\BalanceFlow as BalanceFlowModel;
use think\facade\Event;

/**
 * 账户余额流水接口类
 */
class BalanceFlow extends BalanceFlowModel
{

    /**
     * 流水记录
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-04-21
     *
     * @return   [type]                      [description]
     */
    public function updateFlow($uid, $alter_type, $num, $balance, $rel_type, $rel_pk, $note = '')
    {
        $data = [
            'user_id'    => $uid,
            'alter_type' => $alter_type,
            'num'        => $num,
            'balance'    => $balance,
            'rel_type'   => $rel_type,
            'rel_pk'     => $rel_pk,
            'note'       => $note,
        ];

        if (!$this->save($data)) {
            throw new LogicException('流水添加失败');
        }
        // 流水添加后处理事件 
        Event::subscribe(BalanceSubscribe::class);
        Event::trigger('BalanceUpdata', $data);

        return $this;

    }
}
